package com.atguigu.controller;


import com.atguigu.result.RetVal;
import com.atguigu.search.Product;
import com.atguigu.search.SearchParam;
import com.atguigu.search.SearchResponseVo;
import com.atguigu.service.SearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 品牌表 前端控制器
 * </p>
 */
@RestController
@RequestMapping("/search")
public class SearchController {
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
    private SearchService searchService;

    //1.创建索引与映射
    @GetMapping("createIndex")
    public RetVal createIndex(){
        elasticsearchRestTemplate.createIndex(Product.class);
        elasticsearchRestTemplate.putMapping(Product.class);
        return RetVal.ok();
    }

    //2.商品的上架
    @PostMapping("onSale/{skuId}")
    public String onSale(@PathVariable Long skuId) {
        searchService.onSale(skuId);
        return "success";
    }
    //2.商品的上架
    @PostMapping("offSale/{skuId}")
    public String offSale(@PathVariable Long skuId) {
        searchService.offSale(skuId);
        return "success";
    }
    //3，商品查询
    @PostMapping("searchProduct")
    public RetVal searchProduct(@RequestBody SearchParam searchParam) {
      SearchResponseVo searchResponseVo= searchService.searchProduct(searchParam);
        return RetVal.ok(searchResponseVo);
    }
}

